<?php

/**
 * Database singleton
 * 
 * Returns the database connection as a singleton object.
 * This will serve as a wrapper for different approaches
 * or database abstractions later on.
 * 
 * @author Oliver Schwarz <oliver.schwarz@gmail.com>
 * @package Core
 */
class DB
{

        private static $instance;
        private function __construct()
        {
                throw new Exception('DB: Invoking constructor not allowed');
        }
        public static function getInstance()
        {
                if (!isset(self::$instance)):
                        self::$instance = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
                endif;
                return self::$instance;
        }
        public static function execute($sql)
        {
                $db = DB::getInstance();
                return $db->query($sql);
        }
        public static function get($sql)
        {
                $db = DB::getInstance();
                $res = $db->query($sql);
                if (isset($res)):
                        while ($row = $res->fetch_assoc()):
                                $result[] = $row;
                        endwhile;
                        return $result;
                endif;
                return false;
        }
        protected function __clone()
        {
                throw new Exception('DB: Cloning not allowed');
        }

}

?>